package com.github.yeecode.mybatisdemo.jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author : wushikai
 * <p>
 * date : 2021-01-30
 */
public class JdkProxyUtil implements InvocationHandler {
    private Object target ;//需要代理的目标对象

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JDK动态代理，监听开始！休想拜托我");
        Object result = method.invoke(target, args);
        System.out.println("JDK动态代理，监听结束！休想拜托我");
        return result;
    }


    //定义获取代理对象方法
    private Object getJDKProxy(Object targetObject){
        //为目标对象target赋值
        this.target = targetObject;
        //JDK动态代理只能针对实现了接口的类进行代理，newProxyInstance 函数所需参数就可看出
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this);
    }







}
